<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use App\PostTopic;
use App\Topic;

class TopicController extends Controller
{
    /**
      在专题页面，点击投稿 显示当前用户可投稿的文章
     */
    public function index(Topic $topic)
    {
        //
    //扫全表
      // $a=Post::pan($topic->id)->get();

        $arr=PostTopic::where('topic_id','=',$topic->id)->pluck('post_id');
        $PostTopics=Post::whereIn('id',$arr)->get();
      
    
      $myposts=Post::au(\Auth::id())->topics($topic->id)->get();

        return view('/topic/topic',compact('PostTopics','myposts','topic'));
    }

    /**
        把文章提交到专题
     */
    public function submit(Topic $topic)
    {
        //
        $post_ids=request('post_ids');
        $topic_id=$topic->id;
        foreach ($post_ids as $post_id) {
              PostTopic::create([
                    'post_id'=>$post_id,
                    'topic_id'=>$topic_id,
              ]);

      
        }
          return back();
    }


}
